﻿using HotChocolate.Fusion.Composition;
using HotChocolate.Language;

namespace Gateway.Api.Infrastructures;

public sealed class MicroserviceSubgraph
{
    public MicroserviceSubgraph(string name, Uri httpBaseAddress, DocumentNode schema)
    {
        Name = name;
        HttpBaseAddress = httpBaseAddress;
        Schema = schema;
    }

    public string Name { get; }
    public Uri HttpBaseAddress { get; }
    public DocumentNode Schema { get; }
    public string Extensions { get; }

    public SubgraphConfiguration ToConfiguration()
        => new SubgraphConfiguration(
            Name,
            Schema.ToString(),
            extensions: Extensions,
            new IClientConfiguration[]
            {
                new HttpClientConfiguration(HttpBaseAddress, "DefaultClient"),
                new WebSocketClientConfiguration(new Uri($"ws://{HttpBaseAddress.Host}:{HttpBaseAddress.Port}/graphql"))
            }, 
            null);
}
